﻿using Model;
using Service;
using System;
using System.Collections.Generic;
using System.Linq;
using Utility;

namespace DataProvider
{
    public class Sys_UserProvider
    {
        public static readonly Sys_UserProvider Instances = new Sys_UserProvider();
        public static readonly Sys_UserService Service = Sys_UserService.Instances;

        public Sys_User GetEntity(string UserName)
        {
            var list = Sys_UserService.Instances.QueryCustom("  UserName='" + UserName + "' ");
            if(list.Count>0)
            {
                return list.First();
            }
            else
            {
                return null;
            }
        }

        public List<Sys_User> GetList(UserQuery queryModel, out int total)
        {
            var allList = Service.QueryCustom($" Status={(int)Status.normal}").ToList();

            var conditions = new List<Func<Sys_User, bool>>();

            #region 组装查询
            if (!string.IsNullOrEmpty(queryModel.UserName))
            {
                conditions.Add(x => x.UserName != null && x.UserName.Contains(queryModel.UserName));
            }
            if (!string.IsNullOrEmpty(queryModel.RealName))
            {
                conditions.Add(x => x.RealName != null && x.RealName.Contains(queryModel.RealName));
            }
            if (queryModel.Gender!=null)
            {
                conditions.Add(x => x.Gender ==queryModel.Gender);
            }
            if (queryModel.RoleId!=null && queryModel.RoleId>0)
            {
                conditions.Add(x => x.RoleIds != null && x.RoleIds.Split(',').Contains(queryModel.RoleId.ToString()));
            }
            #endregion

            foreach (var condition in conditions)
            {
                allList = allList.Where(condition).ToList();
            }

            if (queryModel.page > 0 && queryModel.pageSize > 0)
            {
                allList = allList.Skip(queryModel.pageSize * (queryModel.page - 1)).Take(queryModel.pageSize).ToList();
            }

            total = allList.Count();

            return allList;
        }

        public Sys_User Get(int id) => Service.GetEntity(id);

        public int SaveModel(int userId, Sys_User model,out string tipMsg)
        {
            var res = 0;
            tipMsg = "";
            var temp = this.GetEntity(model.UserName);
            if ((model.Id==0 && temp!=null)||(model.Id>0 && temp!=null && model.Id!=temp.Id && temp.Status==Status.normal))
            {
                tipMsg = "The account number is duplicated and failed to add.";
                return res;
            }

            if (model.Id == 0)
            {
                res = Service.Add(userId, model);
            }
            else
            {
                res = Service.Edit(userId, model);
            }
            return res;
        }

        public int Del(int userId, int[] ids)
        {
            return Service.EditBatch(userId, new Sys_User() { Status = Status.delete, LastUpdateUser = userId, LastUpdateTime = DateTime.Now }, ids);
        }
        public List<Sys_User> GetUserListAll(bool IsOnStatus=true)
        {
            string condition = "";
            if(IsOnStatus==false)
            {
                condition = condition + " and Status=0 ";
            }
            return Sys_UserService.Instances.QueryCustom(condition);
        }

    }
}
